package com.zhang.spring.c_prcxy;

import java.lang.reflect.Method;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import com.zhang.spring.Service.userService;
import com.zhang.spring.Service.userServiceimpl;

public class userprcxyFactory02 implements MethodInterceptor {

	public userService getUserServiceFactory() {
		Enhancer en = new Enhancer();// 生成代理对象
		// 告诉目标对象
		en.setSuperclass(userServiceimpl.class);
		en.setCallback(this);
		userService us=(userService)en.create();//创建代理对象
		return us;
	}

	@Override
	public Object intercept(Object obj, Method med, Object[] arg,
			MethodProxy methodProxy) throws Throwable {
		// obj被代理的原始对象---med被代理的原始类--- arg代理对象-----methodProxy被代理的类
		//打开事务
		System.out.println("打开了事务");
		System.out.println("提交事务");
		//调用原始方法
		Object invokevalue = methodProxy.invokeSuper(obj, arg);
		//提交事务
		return invokevalue;
		
	}

}
